package com.spark.incoicings.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.spark.incoicings.dao.ProductMapper;
import com.spark.incoicings.dao.ReturnDetailMapper;
import com.spark.incoicings.service.ReturnDetailService;
import com.spark.invoicings.entity.Product;
import com.spark.invoicings.entity.ReturnDetail;
import com.spark.invoicings.utils.Result;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/login")
public class ReturnDetailController {
    @Resource
    private ReturnDetailService returnDetailService;
    @Resource
    private ReturnDetailMapper detailMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private RedisTemplate redisTemplate;
    /**
     * 查询所有退货明细
     * @param pageNum
     * @param pageSize
     * @param id
     * @param productName
     * @return
     */
    @GetMapping("/selectReturnDetailAll")
    public Result selectReturnDetailAll(@RequestParam(defaultValue = "1")Integer pageNum,
                                        @RequestParam(defaultValue = "3")Integer pageSize,
                                        @RequestParam(required = false)Integer id,
                                        @RequestParam(required = false)String productName){
        PageHelper.startPage(pageNum,pageSize);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("productName",productName);
        List<ReturnDetail> returnDetails = returnDetailService.selectReturnDetailAll(map);
        PageInfo<ReturnDetail> pageInfo = new PageInfo<>(returnDetails);
        return Result.success(200,"success",pageInfo);
    }
    @GetMapping("/selectReturnDetailProduct")
    public Result selectReturnDetailProduct(Integer id){
        List<ReturnDetail> returnDetails = detailMapper.selectList(new QueryWrapper<ReturnDetail>().eq("return_order_id", id));
        returnDetails.forEach(returnDetail -> {
            Product product = productMapper.selectById(returnDetail.getProductId());
            returnDetail.setProduct(product);
        });
        return Result.success(200,"success",returnDetails);
    }
    @GetMapping("/getRedisStock")
    public Result getRedisStock(Integer id){
        Integer o =(Integer) redisTemplate.opsForValue().get("product_stack" + ":" + id);
        return Result.success(200,"success",o);
    }
}
